﻿using famails.bus;
using famails.common;
using famails.dto;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace famails.BackEnd.Blog
{
    public partial class ArticleList : System.Web.UI.Page
    {
        static UserLoginDTO userLogin = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            userLogin = getUserLogin();
            if (!IsPostBack)
            {
                LoadCategory();
                LoadGroup();
            }
        }

        private UserLoginDTO getUserLogin()
        {
            if (Session["us-login"] != null)
            {
                return (UserLoginDTO)Session["us-login"];
            }
            else Response.Redirect("~");
            return null;
        }
        private void LoadCategory()
        {
            ddkCategory.DataTextField = "CategoryName";
            ddkCategory.DataValueField = "CategoryID";
            CategoryBUS cBus = new CategoryBUS();
            DataTable source = cBus.GetAll("", -1);

            DataRow row = source.NewRow();
            row["categoryid"] = -1;
            row["categoryname"] = "Tất cả";
            source.Rows.InsertAt(row, 0);
            ddkCategory.DataSource = source;
            ddkCategory.DataBind();
        }
        void LoadGroup()
        {
            ddlGroup.DataTextField = "GroupName";
            ddlGroup.DataValueField = "GroupID";
            GroupBUS gBus = new GroupBUS();

            int id = -1;
            int.TryParse(ddkCategory.SelectedValue + "", out id);

            DataTable source = gBus.GetAll("", -1, id);

            DataRow row = source.NewRow();
            row["groupid"] = -1;
            row["groupname"] = "Tất cả";
            source.Rows.InsertAt(row, 0);
            ddlGroup.DataSource = source;
            ddlGroup.DataBind();
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static List<ArticleDTO> Search(string keyword, int show, int categoryid, int groupid)
        {
            //string name = "", phone = "h", email = ""; int group = -1;
            ArticleBUS ctBUS = new ArticleBUS();
            DataTable customerBySelect = ctBUS.Search(keyword, show, categoryid, groupid);
            List<ArticleDTO> result = new List<ArticleDTO>();
            ArticleDTO item = new ArticleDTO();
            foreach (DataRow dr in customerBySelect.Rows)
            {
                item = new ArticleDTO();
                item.ArticleID = Convert.ToInt32(dr["ArticleID"] + "");
                item.Title = dr["Title"] + "";
                item.Summary = dr["Summary"] + "";
                item.CategoryName = dr["CategoryName"] + "";
                item.GroupName = dr["GroupName"] + "";
                item.Show = dr["Show"] + "";
                result.Add(item);
            }

            return result;
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static void Delete(int id)
        {
            try
            {
                ArticleBUS Bus = new ArticleBUS();
                Bus.Delete(id);

            }
            catch (Exception ex)
            {
                Common.logs(userLogin.Username + "-BlogCatetory-Delete-", ex, Common.logType.error);
            }

        }
    }
}